#! /bin/bash

function show_tables()
{
    tables=`echo ".q" | sqlite3 trace.db -cmd '.tables'`
    echo "All Tables:"
    echo "------------------"
    echo "$tables"
    for table in $tables
    do
        echo
        echo
        echo "[$table]"
        echo "------------------"
        echo -e ".schema $table\n.q\n" | sqlite3 trace.db
        echo "------------------"
        echo ".q" | sqlite3 trace.db -cmd "PRAGMA table_info(analyse)"
    done
}

case $1 in
    0)
        show_tables
        ;;
    1)
        echo ".q" | sqlite3 trace.db -cmd 'select * from trace'
        ;;
    2)
        echo "show free (nil)"
        echo ".q" | sqlite3 trace.db -cmd 'select * from trace where ptr="(nil)"'
        ;;
    3)
        echo "md5: trace table"
        echo ".q" | sqlite3 trace.db -cmd 'select * from trace' | md5sum
        ;;
    4)
        echo ".q" | sqlite3 trace.db -cmd 'select distinct task from analyse group by task' # get different task
        ;;
    5)
        echo ".q" | sqlite3 trace.db -cmd 'select distinct task,pid from analyse group by task,pid'
        ;;
    6)
        echo ".q" | sqlite3 trace.db -cmd 'select COUNT (flag) from trace where flag="N"'
        ;;
esac
